/*
 * File:   11492.cpp
 * Author: xerxes
 *
 * Created on September 18, 2011, 11:49 AM
 *
 * I have not failed, I have just found 10000 ways that won't work.
 */

#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define foR(i,st,ed) for(int i = st ; i < ed ; ++i )
#define fo( i, ed ) foR( i , 0 , ed )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define _f first
#define _s second.first
#define _t second.second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,char> >
#define mmk( X, Y, Z ) mk( X , mk( Y, Z ) )

inline void wait(double seconds) {
    double endtime = clock()+(seconds * CLOCKS_PER_SEC);
    while (clock() < endtime) {
        ;
    }
}

char buff1[100];
char buff2[100];
char buff3[100];
int m;
inline int LEN(string ss){
    int dd=0;
    for(int i=0,j=LN(ss); i<j; ++i){
        if( ss[i]!=' '){
            ++dd;
        }
    }
    return dd;
}
int source,sink;
int x;

vector< vector< tp > > cango;
int best;
inline void Read(){
    scanf("%s %s",buff1,buff2);
    //cout<<buff1<<" "<<buff2<<"\n";
    map< string , int > id;
    x=0;

    string sl = (string)buff1;
    string dl = (string)buff2;
    if( !id.count(sl) ){
        id[sl]=x;
        ++x;
    }
    if( !id.count(dl) ){
        id[dl]=x;
        ++x;
    }
    
    source= id[sl];
    sink = id[dl];
    
    vector< int > v1,v2,v3;
    vector< char> v4;

    string s,d,w;
    fo(i,m){
        scanf("%s %s %s",buff1,buff2,buff3);
        //cout<<buff1<<" "<<buff2<<" "<<buff3<<"\n";
        s = (string)buff1;
        d = (string)buff2;
        w = (string)buff3;
        if( !id.count(s) ){
            id[s]=x;
            ++x;
        }
        if( !id.count(d) ){
            id[d]=x;
            ++x;
        }

        int sx = id[s];
        int sy = id[d];
        v1.pb(sx);
        v2.pb(sy);
        
        v3.pb( LEN(w) );
        v4.pb( w[0] );
    }
    //cout<<"total Number of Languages : "<<x<<"\n";
    cango.assign(x+5, vector<tp >() );
    fo(i,m){
        int sx = v1[i];
        int sy = v2[i];
        int ln = v3[i];
        char ch= v4[i];
        cango[sx].pb( mmk(sy,ln,ch) );
        cango[sy].pb( mmk(sx,ln,ch) );
        //cout<<sx<<" "<<sy<<" "<<ln<<" "<<ch<<"\n";
    }
}
int D[3000][200];
int vis[3000][200];

#define box pair< int , pair< int, char > >
inline void dijkstra(int ssx, int ddy){
    //initialization begins
    set< box, less<box > > pq;
    SET(vis,0);
    fo(i,3000){
        fo(j,200){
            D[i][j] = (int)1023456789;
        }
    }
    //initialization begin ends

    pq.insert( mmk(0,ssx,'~') );
    D[ssx]['~'-0]=0;
    while( pq.empty()==false ){
        int cost = pq.begin()->_f;
        int father=pq.begin()->_s;
        char ch=pq.begin()->_t;
        pq.erase( pq.begin() );
        if( father==ddy ){
            best = min( best, cost );
        }
        if( vis[ father ][ch-0 ] )continue;
        //printf("FOR FATHER : COST : CHAR %d %d %c : \n",father,cost,ch);
        vector< tp > go = cango[ father ];
        for(int i=0,j=SZ(go); i<j; ++i ){
            int ccost = go[i]._s;
            int child = go[i]._f;
            char cchar = go[i]._t;
            if( cchar == ch )continue;
            if( vis[child][cchar-0] )continue;
            if( cost+ccost < D[ child ][ cchar-0 ] ){
                D[child][cchar-0] = cost+ccost;
                //printf("Inserting CHILD , COST , CHAR %d %d %c\n",child,ccost,cchar);
                pq.insert( mmk(D[child][cchar-0],child,cchar ) );
            }
        }

        vis[ father ][ch-0 ] =1;
    }
}
inline void Proc(){
//    cout<<"NEW : \n";
//    fo(i,x){
//        vector< tp > go = cango[i];
//        fo(j,SZ(go)){
//            cout<<go[j]._f<<" "<<go[j]._s<<" "<<go[j]._t<<"\n";
//        }
//        cout<<"\n";
//    }
//    cout<<"DONE\n";
    best = (int)1023456789;
    dijkstra(source,sink);
    if( best<(int)1023456789 ){
        cout<<best<<"\n";
    }
    else{
        cout<<"impossivel\n";
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "rt", stdin);
#endif
    
    while( scanf("%d",&m)==1 ){
        if(m==0)break;
        Read();
        Proc();
    }

    return 0;
}


